﻿/* ######################################## *\
 * ### Copyright (C) 2009 AJ Ravindiran ### *
\* ######################################## */
using System;
using System.IO;
using System.Net.Sockets;

using AJRavindiran.Jolt.RuneScape.Models.Characters;

namespace AJRavindiran.Jolt.RuneScape.Network
{
    /// <summary>
    /// Represents a player socket.
    /// </summary>
    public class PlayerSocket
    {
        #region Fields
        private Character character;
        private TcpClient tcpClient;
        private Socket socket;
        private System.IO.Stream stream;
        #endregion Fields

        #region Properties
        /// <summary>
        /// Gets how much data is availible in the tcp client.
        /// </summary>
        public int Availible
        {
            get
            {
                try
                {
                    if (tcpClient == null)
                        return 0;

                    return tcpClient.Available;
                }
                catch (Exception ex)
                {
                    Jolt.GetLog().WriteException(ex);
                    throw ex;
                }
            }
        }

        public System.IO.Stream Stream
        {
            get { return stream; }
            set { stream = value; }
        }

        public TcpClient TcpClient
        {
            get { return tcpClient; }
            set { tcpClient = value; }
        }
        #endregion Properties

        #region Constructors
        /// <summary>
        /// Contructs a new player socket class.
        /// </summary>
        public PlayerSocket(Character character, Socket socket)
        {
            try
            {
                this.socket = socket;
                this.character = character;

                this.tcpClient = new TcpClient();
                this.tcpClient.Client = socket;
                this.stream = tcpClient.GetStream();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region Destructor
        /// <summary>
        /// Destruct the player socket class.
        /// </summary>
        ~PlayerSocket()
        {
            this.stream.Close();
            this.tcpClient.Close();
            this.socket.Close();
        }
        #endregion Destructor

        #region Methods
        /// <summary>
        /// Read one byte from the socket.
        /// </summary>
        public int Read()
        {
            try
            {
                if (tcpClient.Available < 1 || tcpClient == null)
                    return -1;
                return stream.ReadByte();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int ReadAsync()
        {
            try
            {
                if (tcpClient.Available < 1 || tcpClient == null)
                    return -1;
                return stream.ReadByte();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Read multiple bytes from the socket.
        /// </summary>
        public int Read(int read)
        {
            try
            {
                if (tcpClient == null)
                    return -1;
                return stream.Read(character.GetStreamReader().InBuffer, 0, read);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Write a byte to the remote host.
        /// </summary>
        /// <param name="b"></param>
        public void Write(byte b)
        {
            try
            {
                if (tcpClient == null)
                    return;
                stream.WriteByte(b);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Write bytes to the remote host.
        /// </summary>
        /// <param name="array"></param>
        public void Write(byte[] array, int startPosition, int finishPosition)
        {
            try
            {
                if (tcpClient == null)
                    return;
                stream.Write(array, startPosition, finishPosition);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Write any pending bytes to the client.
        /// </summary>
        public void WriteBuffer()
        {
            try
            {
                if (character.GetStreamWriter().WriteOffset > 0)
                    stream.Write(character.GetStreamWriter().OutBuffer, 0, character.GetStreamWriter().WriteOffset);

                character.GetStreamWriter().WriteOffset = 0;
                this.Flush();
            }
            catch (Exception ex)
            {
                character.GetStatus().Disconnected[0] = true;
                throw ex;
            }
        }

        /// <summary>
        /// Flush the outstream.
        /// </summary>
        public void Flush()
        {
            try
            {
                if (socket == null)
                    return;
                stream.Flush();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion Methods
    }
}
